part of '../../repository.dart';

class _YookassaTransactionDtoFactory {
  const _YookassaTransactionDtoFactory._internal();

  static const _instance = _YookassaTransactionDtoFactory._internal();

  factory _YookassaTransactionDtoFactory() {
    return _instance;
  }

  _YookassaTransactionDto fromJson(Map<String, dynamic> json) {
    final type = json[_kWebTransactionTypeKey] as String;

    if (type == _kWebOneTimePurchasedItemTransactionTypeValue) {
      return _YookassaOneTimePurchasedItemTransactionDto.fromJson(json);
    }

    if (type == _kWebPurchasedSubscriptionTransactionTypeValue) {
      return _YookassaPurchasedSubscriptionTransactionDto.fromJson(json);
    }

    throw UnimplementedError();
  }

  _YookassaTransactionDto fromEntity(YookassaTransaction yookassaTransaction) {
    if (yookassaTransaction is YookassaOneTimePurchasedItemTransaction) {
      return _YookassaOneTimePurchasedItemTransactionDto.fromEntity(yookassaTransaction);
    }

    if (yookassaTransaction is YookassaPurchasedSubscriptionTransaction) {
      return _YookassaPurchasedSubscriptionTransactionDto.fromEntity(yookassaTransaction);
    }

    throw UnimplementedError();
  }
}
